﻿using HalconDotNet;
using MotionControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WHW_NetUtility;

namespace MainFrame
{

    public partial class MainFormWG : Form
    {
        public MainFormWG()
        {
            InitializeComponent();
        }
        //public void MessageDisplay(string message)
        //{
        //    try
        //    {
        //        rbt_runlog.BeginInvoke(new MethodInvoker(() =>
        //        {
        //            if (rbt_runlog.Lines.Length > 2000)
        //            {
        //                rbt_runlog.Text = "";
        //            }
        //            DateTime NowTime = DateTime.Now;
        //            rbt_runlog.AppendText(NowTime.ToString("yyyy/MM/dd HH:mm:ss.fff") + Environment.NewLine + message + Environment.NewLine);
        //            rbt_runlog.ScrollToCaret();
        //        }));
        //        Log4Helper.WriteLog("界面信息", message);
        //    }
        //    catch (Exception)
        //    {
        //    }

        //}
        //public void MessageDisplay(string message, string english = "")
        //{
        //    try
        //    {

        //        rbt_runlog.BeginInvoke(new MethodInvoker(() =>
        //        {
        //            if (rbt_runlog.Lines.Length > 1000)
        //            {
        //                rbt_runlog.Text = "";
        //            }
        //            DateTime NowTime = DateTime.Now;
        //            rbt_runlog.AppendText(NowTime.ToString("yyyy/MM/dd HH:mm:ss.fff") + Environment.NewLine + message + Environment.NewLine);
        //            rbt_runlog.ScrollToCaret();
        //        }));



        //        Log4Helper.WriteLog("界面信息", message);
        //    }
        //    catch (Exception)
        //    {
        //    }

        //}
        //public void AlarmMessageDisplay(string message)
        //{
        //    try
        //    {
        //        string str = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + "  " + message + Environment.NewLine;
        //        rbt_alarmlog.BeginInvoke(new MethodInvoker(() =>
        //        {
        //            if (rbt_alarmlog.Lines.Length > 1000)
        //            {
        //                rbt_alarmlog.Text = "";
        //            }
        //            rbt_alarmlog.SelectionColor = Color.Red;
        //            rbt_alarmlog.SelectedText = str;
        //            rbt_alarmlog.ScrollToCaret();

        //        }));
        //        Log4Helper.WriteLogError(message);
        //    }
        //    catch (Exception)
        //    {
        //    }

        //}
        List<string> okarr = new List<string> ();
        public void UserLogin()
        {
            okarr = new List<string>();
            var NGarr = new List<string> { };
            this.Invoke(new Action(() => {

                if (MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.operatorStaff)//操作
                {
                    okarr.Add("主界面");
                    okarr.Add("手动界面");
            //        okarr.Add("工艺配置");
             //      okarr.Add("设备参数");
             //       okarr.Add("调试信息");
                    okarr.Add("报警界面");
                    okarr.Add("IO查询");
        //            okarr.Add("产品切换");
                    okarr.Add("产品数据");
                 //   okarr.Add("权限设定");
                    //NGarr.Add("工艺配置");
                    //NGarr.Add("设备参数");
                    //NGarr.Add("调试信息");
                }
                else if (MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.technician)//工艺
                {
                    okarr.Add("主界面");
                    okarr.Add("手动界面");
                    okarr.Add("工艺配置");
                    okarr.Add("设备参数");
                    okarr.Add("报警界面");
                    okarr.Add("IO查询");
                    okarr.Add("产品切换");
                    okarr.Add("产品数据");
                //    okarr.Add("权限设定");
                 //   okarr.Add("调试信息");
                }
                else if (MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.admin)//工程师
                {
                    okarr.Add("主界面");
                    okarr.Add("手动界面");
                    okarr.Add("工艺配置");
                    okarr.Add("设备参数");
                    okarr.Add("功能参数");
                    okarr.Add("报警界面");
                    okarr.Add("IO查询");
                    okarr.Add("产品切换");
                    okarr.Add("产品数据");
                    okarr.Add("权限设定");

                }
                else if (MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.superAdmin)//管理员
                {
                    okarr.Add("主界面");
                    okarr.Add("手动界面");
                    okarr.Add("工艺配置");
                    okarr.Add("设备参数");
                    okarr.Add("功能参数");
                    okarr.Add("报警界面");
                    okarr.Add("IO查询");
                    okarr.Add("产品切换");
                    okarr.Add("产品数据");
                    okarr.Add("权限设定");

                }
                btn_mains.ForEach(a=>
                {
                    if (okarr.Contains(a.Text))
                        a.Enabled = true;
                    else
                        a.Enabled = false;
                }
                   
                );
              
            }));


        }
      



        private List<Button> btn_mains = new List<Button>();
        private void MainFormWG_Load(object sender, EventArgs e)
        {
          
          
           PageLoadTip loadTip = new PageLoadTip();
            loadTip.Show();
           
            foreach (var curControl in tableLayoutPanel2.Controls)
            {
                if (curControl is Button)
                {
                    var curButton = (Button)curControl;
                //    curButton.Click += ButtonMain_Click;
                    btn_mains.Add((Button)curControl);
                }
            }
            try
            {
                GlobalMotion.AxisHome += GlobalMotion.GoHome_Await;
                Log4Helper.init();
                int hegihtView = Screen.PrimaryScreen.Bounds.Height;
                int hegihtWork = Screen.PrimaryScreen.WorkingArea.Height;
                #region 视觉矫正参数
                visionProc.hv_CameraParameters = new HTuple();
                visionProc.hv_CameraParameters[0] = "area_scan_division";
                //visionProc.hv_CameraParameters[1] = 7.20549e-05;
                //visionProc.hv_CameraParameters[2] = -2018.14;
                //visionProc.hv_CameraParameters[3] = 2.40073e-06;
                //visionProc.hv_CameraParameters[4] = 2.4e-06;
                //visionProc.hv_CameraParameters[5] = 2744.64;
                //visionProc.hv_CameraParameters[6] = 1707.22;
                //visionProc.hv_CameraParameters[7] = 5472;
                //visionProc.hv_CameraParameters[8] = 3648;
                visionProc.hv_CameraParameters[1] = 0.0157154;
                visionProc.hv_CameraParameters[2] = -1842.42;
                visionProc.hv_CameraParameters[3] = 2.40224e-06;
                visionProc.hv_CameraParameters[4] = 2.4e-06;
                visionProc.hv_CameraParameters[5] = 2716.02;
                visionProc.hv_CameraParameters[6] = 1703.52;
                visionProc.hv_CameraParameters[7] = 5472;
                visionProc.hv_CameraParameters[8] = 3648;
                HOperatorSet.ChangeRadialDistortionCamPar("adaptive", visionProc.hv_CameraParameters, 0,
             out visionProc.hv_CameraZero);
                HOperatorSet.ReadObject(out visionProc.mapObject, Application.StartupPath + "\\mapObj.hobj");
                #endregion
               
                changeUser.changeUserMain += UserLogin;

                Log4Helper.WriteLog("程序启动");
                loadTips.loadTipsInLogin("加载主窗体");
                threadPoolSet();//线程设置
 
                GlobalVar.isExit = false;
                #region load close software data
                try
                {
                    GlobalVar.closeSave = (closeStartConifg)SerializeHelper.DeserializeFile<closeStartConifg>(GlobalVar.closeSavePath, "关闭保存");//串口数据反序列化
                    if (GlobalVar.closeSave == null)
                        GlobalVar.closeSave = new closeStartConifg();
                    if (!string.IsNullOrEmpty(GlobalVar.closeSave.CurrentTypeName))
                        GlobalVar.currentSelectTypeName = GlobalVar.closeSave.CurrentTypeName;
                    else
                        GlobalVar.closeSave.CurrentTypeName = "123^ver2";

                }
                catch (System.Exception ex)
                {
                    Log4Helper.WriteLog("载入上次关闭时保存的文件失败：" + ex.Message);
                    Log4Helper.WriteLog(ex);
                }
                if (GlobalVar.closeSave.OpenFirstTime_today == null || GlobalVar.closeSave.OpenFirstTime_today.Date < DateTime.Now.Date)
                {
                    //GlobalVar.closeSave.NGCount = 0;
                    //GlobalVar.closeSave.OKCount = 0;
                    GlobalVar.closeSave.stopTime_today = DateTime.Now.Date;
                    GlobalVar.closeSave.runTime_today = DateTime.Now.Date;
                    GlobalVar.closeSave.EstopTime_today = DateTime.Now.Date;
                    GlobalVar.closeSave.OpenFirstTime_today = DateTime.Now;
                }
                #endregion
                
                #region serial port init
                loadTips.loadTipsInLogin("串口初始化");
                GlobalVar.userConfig = (ConfigData)SerializeHelper.DeserializeFile<ConfigData>(GlobalVar.configPath, "用户配置数据");//串口数据反序列化
                #endregion
                dispalyPage(e_ButtonMain.主界面);
                dispalyPage(e_ButtonMain.功能参数);
                if (GlobalVar.RFIDBox_in != null && GlobalVar.RFIDBox_out != null)
                {
                    GlobalVar.RFIDBox_in.displayInfoMainBox += disPlayRFIDLog;
                    GlobalVar.RFIDBox_out.displayInfoMainBox += disPlayRFIDLog;
                }
                MotionControl.GlobalVariable.OperLevel = (int)MotionControl.operLevel.operatorStaff;
                #region 用户身份
                changeUser.changeUserMain();
                #endregion
                dispalyPage(e_ButtonMain.产品切换);
                loadTips.loadTipsInLogin("窗口初始化");
                #region 控制卡检测
                GlobalVar.isHardStart = GlobalMotion.cardInit();
                #endregion
                #region 窗口设置
                loadTips.loadTipsInLogin("加载数据显示窗体");
                foreach (var curButton in btn_mains)
                {
                    var e_button = (e_ButtonMain)Enum.Parse(typeof(e_ButtonMain), curButton.Text);
                    dispalyPage(e_button);
                    Application.DoEvents();
                }
                dispalyPage(e_ButtonMain.主界面);
                Task.Run(ConnectSQL);
                #endregion
                #region 弹窗条件初始化
                //弹窗条件初始化
                for (int i = 0; i < GlobalVar.errorTips.Length; i++)
                {
                    GlobalVar.errorTips[i] = new warningErrorInfo();
                }
                for (int i = 0; i < GlobalVar.warningTips.Length; i++)
                {
                    GlobalVar.warningTips[i] = new warningErrorInfo();
                }
                #endregion
              
                #region 定位相机图片显示
                loadTips.loadTipsInLogin("图片信息路径的配置有误！");
                var imgDir1 = Application.StartupPath + $"\\saveImage\\{e_CameraPlate.定位相机平台1.ToString()}";
                var fs1 = Directory.GetFiles(imgDir1);
                if (fs1.Length > 0)
                {
                    if (File.Exists(fs1[0]))
                    {
                        HOperatorSet.ReadImage(out visionProc.srcObject_pos, fs1[0]);
                        GlobalVar.imageDisplay1?.Invoke(visionProc.srcObject_pos);
                    }
                }
                var imgDir2 = Application.StartupPath + $"\\saveImage\\{e_CameraPlate.图案相机平台1.ToString()}";
                var fs2 = Directory.GetFiles(imgDir2);
                if (fs2.Length > 0)
                {
                    if (File.Exists(fs2[0]))
                    {
                        HOperatorSet.ReadImage(out visionProc.srcObject_scan, fs2[0]);
                        GlobalVar.imageDisplay2?.Invoke(visionProc.srcObject_scan);
                    }
                }
                var imgDir3 = Application.StartupPath + $"\\saveImage\\{e_CameraPlate.检测相机平台1.ToString()}";
                var fs3 = Directory.GetFiles(imgDir3);
                if (fs3.Length > 0)
                {
                    if (File.Exists(fs1[0]))
                    {
                        HOperatorSet.ReadImage(out visionProc.srcObject_ckeck, fs3[0]);
                        GlobalVar.imageDisplay3?.Invoke(visionProc.srcObject_ckeck);
                    }
                }
                visionProc.ReadRegion();
                visionProc.ROI1 = visionProc.ROI_List[(int)e_CameraPlate.定位相机平台1];
                visionProc.ROI2 = visionProc.ROI_List[(int)e_CameraPlate.图案相机平台1];
                visionProc.ROI_M_List1 = visionProc.ReadRegion_M(e_CameraPlate.检测相机平台1);
                visionProc.ROI_M_List2 = visionProc.ReadRegion_M(e_CameraPlate.检测相机平台2);
                GlobalVar.imageDisplay1_obj?.Invoke(visionProc.ROI1.ROI_Region, "green");
                GlobalVar.imageDisplay1_obj?.Invoke(visionProc.ROI1.ROI_Template, "blue");
                GlobalVar.imageDisplay2_obj?.Invoke(visionProc.ROI2.ROI_Region, "green");
                GlobalVar.imageDisplay2_obj?.Invoke(visionProc.ROI2.ROI_Template, "blue");
                visionProc.ReadTemplateRegion();
                for (int m = 0; m < visionProc.ROI_M_List1.Count; m++)
                {
                    if(m<10)
                    GlobalVar.imageDisplay3_obj?.Invoke(visionProc.ROI_M_List1[m], "green");
                }
                for (int m = 0; m < visionProc.ROI_M_List2.Count; m++)
                {
                    if (m < 10)
                        GlobalVar.imageDisplay3_obj?.Invoke(visionProc.ROI_M_List2[m], "green");
                }
                #endregion
             
                loadTips.loadTipsInLogin("线程设定");

                if (MotionControl.GlobalVariable.isCardStart[0])
                {
                    #region 线程启动
                    Task.Factory.StartNew(GlobalMotion.Task_ReadInput);
                    Task.Factory.StartNew(GlobalMotion.Task_ReadOutput);
                    Task.Factory.StartNew(GlobalMotion.Task_ConverFront);
                    Task.Factory.StartNew(GlobalMotion.Task_ConveryUp);

                    //飞达1
                    Task.Factory.StartNew(GlobalMotion.Task_FlyLabel1);
                    Task.Factory.StartNew(GlobalMotion.Task_FlyLabel2);
                    //撕膜
                    Task.Factory.StartNew(GlobalMotion.Task_Cuting);
                    //等待撕膜数据处理
                    Task.Factory.StartNew(GlobalMotion.Task_ConveryCut);
                    //从一工位流动到贴膜位置
                    Task.Factory.StartNew(GlobalMotion.Task_ConveryLabel);
                    //贴膜
                    Task.Factory.StartNew(GlobalMotion.Task_Labeling);
                    //下层输送
                    Task.Factory.StartNew(GlobalMotion.Task_ConveryDown);
                    Task.Factory.StartNew(GlobalMotion.Task_ConveryDownCheck);
                    //NG输送
                    Task.Factory.StartNew(GlobalMotion.Task_NGConvery);
                    Task.Factory.StartNew(GlobalMotion.Task_ProductState);
                    Task.Factory.StartNew(GlobalMotion.Task_CheckPCS);
                    Task.Factory.StartNew(GlobalMotion.Task_air);
                    Task.Factory.StartNew(GlobalMotion.Task_air_cut);
                    Thread.Sleep(100);
                    Task.Factory.StartNew(GlobalMotion.ButtonControl);
                    Task.Factory.StartNew(GlobalMotion.Task_MesTcp);
                    Task.Factory.StartNew(GlobalMotion.Task_LightDisplay);
                    Task.Factory.StartNew(GlobalMotion.Task_MesUrl);
                    Task.Factory.StartNew(GlobalMotion.Task_ConverMan);
                    Task.Factory.StartNew(Task_ButtonControl);
                    Task.Factory.StartNew(GlobalMotion.Task_CheckCard);
                    Task.Factory.StartNew(GlobalMotion.Task_CheckStop);
                    Task.Factory.StartNew(GlobalMotion.Task_EndWork);
                    #endregion
                    GlobalMotion.CardExt_2?.setOut((ushort)e_output_extCard2.LED照明灯, e_out_OnOff.off);
                }
                Task.Factory.StartNew(GlobalMotion.Task_AlarmList);
                loadTip.Close();
                Task.Run(new Action(() => {
                    if (GlobalVar.userConfig.UploadInfo.IsUploadUrl)
                    {
                        if (!string.IsNullOrEmpty(GlobalVar.userConfig.UploadInfo.url_GetDate))
                        {
                            UploadInterface.upload_GetDate();
                        }
                    }

                }));

                //20241207修改更换海康相机屏蔽现有相机连接
                //if (GlobalVar.cameraList != null && GlobalVar.cameraList.Count > 0)
                //{
                //    var arr = GlobalVar.cameraList.Where(a => !a.isInitSuccess).ToArray();
                //    if (arr.Length > 0)
                //    {
                //        string str = "相机未连接  请重启软件";
                //        displalyInfo.displayErrorMain(str);
                //        MessageBox.Show(str);
                //    }
                //}
            }
            catch (System.Exception ex)
            {
                Log4Helper.WriteLogError("程序启动异常：" + ex.Message);
            }

        }
        private void ConnectSQL()
        {
            try
            {
                GlobalVar.sqlHelper = new sqlHelperDapper(dbType.mysql, GlobalVar.userConfig.db_Name, GlobalVar.userConfig.db_UserName,
              GlobalVar.userConfig.db_Password, GlobalVar.userConfig.db_IP);
            }
            catch(Exception ex)
            {
                displalyInfo.displayErrorMain(ex.ToString());
            }
          

        }
        private void disPlayRFIDLog(string log)
        {
            displalyInfo.displayInfoMain?.Invoke(log);
        }
        private void Task_ButtonControl()
        {
            try
            {
                var nstop = -1;
             
                var sw_man = new Stopwatch();
                sw_man.Restart();
                bool isFirst = true;
                while (!GlobalVar.isExit)
                {

                    Thread.Sleep(300);
                   
                    if(GlobalVar.IsStatusAuto&& MotionControl.GlobalVariable.OperLevel != (int)MotionControl.operLevel.superAdmin)
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            btn_mains.ForEach(a => {
                                if (a.Text != "产品数据" && a.Text != "报警界面" && a.Text != "主界面" && a.Text != "IO查询")
                                    a.Enabled = false;
                            });
                        }));
                    }
                    else
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            btn_mains.ForEach(a => {
                                if (okarr.Contains(a.Text))
                                    a.Enabled = true;
                                else
                                    a.Enabled = false;
                            });
                        }));
                    }
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain("Task_ButtonControl" + ex.Message);
            }
        }
        private void threadPoolSet()
        {
            try
            {
                int workThread_min = 0, IOthread_min = 0;
                int workThread_max = 0, IOthread_max = 0;

                ThreadPool.GetMinThreads(out workThread_min, out IOthread_min);
                ThreadPool.GetMaxThreads(out workThread_max, out IOthread_max);
                Log4Helper.WriteLog($"线程池默认最小、最大线程数量：[{workThread_min}-{IOthread_min}]、[{workThread_max}-{IOthread_max}]");
                //MessageDisplay($"线程池默认最大线程数量：{workThread_max}-{IOthread_max}");
                ThreadPool.SetMinThreads(workThread_max - 10, IOthread_max - 5);
                ThreadPool.GetMinThreads(out workThread_min, out IOthread_min);
                //tbx_thread_number_all.Text = workThread_min.ToString();
                Log4Helper.WriteLog($"线程池修改后最小线程数量：{workThread_min}-{IOthread_min}");
                //MessageDisplay($"线程池修改后最大线程数量：{workThread_max}-{IOthread_max}");
            }
            catch (System.Exception ex)
            {
               
                Log4Helper.WriteLog("threadPoolSet", ex);
            }

        }
        private PageMain MainPage = new PageMain();
        private PageVersion VersionPage = new PageVersion();
        private PagePermission PermissionPage = new PagePermission();
        private PageAlarmInfo AlarmPage= new PageAlarmInfo();
        private PageManual ManuaPage = new PageManual();
        private PageConfig ConfigPage = new PageConfig();
        private PageType TypePage = new PageType();
        private PageVision visionPage = new PageVision();
        private PageProductQuery ProductPage = new PageProductQuery();
        private PageIONew IOPage = new PageIONew();
        private void ButtonMain_Click(object sender, EventArgs e)
        {
            var curButton = (Button)sender;
            btn_mains.Where(a => a.Name != curButton.Name).ToList().ForEach(a => a.BackColor =menu_backColor);
            curButton.BackColor = menu_selectColor;
            var e_button=(e_ButtonMain)Enum.Parse(typeof(e_ButtonMain), curButton.Text);
            dispalyPage(e_button);
        }
        public Color menu_selectColor = SystemColors.InactiveCaption;
        public Color menu_backColor = SystemColors.ActiveCaption;
        private void dispalyPage(e_ButtonMain e_Button)
        {
            Form pageForm = null;
            foreach (Control ctr in pl_main.Controls)
            {
                ctr.Hide();
            }
            switch (e_Button)
            {
                case e_ButtonMain.主界面:
                    if (MainPage == null || MainPage.IsDisposed)
                        MainPage = new PageMain();
                    pageForm = MainPage;
                    break;
                case e_ButtonMain.产品切换:
                    if (VersionPage == null || VersionPage.IsDisposed)
                        VersionPage = new PageVersion();
                    pageForm = VersionPage;
                    break;
                case e_ButtonMain.IO查询:
                    if (IOPage == null || IOPage.IsDisposed)
                        IOPage = new PageIONew();
                    pageForm = IOPage;
                    break;
                case e_ButtonMain.工艺配置:
                    if (visionPage == null || visionPage.IsDisposed)
                        visionPage = new PageVision();
                    pageForm = visionPage;
                    break;
                case e_ButtonMain.设备参数:
                    if (TypePage == null || TypePage.IsDisposed)
                        TypePage = new PageType();
                    pageForm = TypePage;
                    break;
                case e_ButtonMain.手动界面:
                    if (ManuaPage == null || ManuaPage.IsDisposed)
                        ManuaPage = new PageManual();
                    pageForm = ManuaPage;
                    break;
                case e_ButtonMain.功能参数:
                    if (ConfigPage == null || ConfigPage.IsDisposed)
                        ConfigPage = new PageConfig();
                    pageForm = ConfigPage;
                    break;
                case e_ButtonMain.报警界面:
                    if (AlarmPage == null || AlarmPage.IsDisposed)
                        AlarmPage = new PageAlarmInfo();
                    pageForm = AlarmPage;
                    break;
                case e_ButtonMain.产品数据:
                    if (ProductPage == null || ProductPage.IsDisposed)
                        ProductPage = new PageProductQuery();
                    pageForm = ProductPage;
                    break;
                case e_ButtonMain.权限设定:
                    GlobalVar.IsUserLogOk = true;
                    if (GlobalVar.IsUserLogOk)
                    {
                        if (PermissionPage == null || PermissionPage.IsDisposed)
                            PermissionPage.Close();
                        PermissionPage = new PagePermission();
                        pageForm = PermissionPage;
                    }
                    else
                    {
                        pageForm = null;
         
                    }
                    logTime++;
                    break;
            }
            if (pageForm != null)
            {
                pageForm.TopLevel = false;
                pageForm.FormBorderStyle = FormBorderStyle.None;
                pageForm.Parent = pl_main;
                pageForm.Dock = DockStyle.Fill;
                pageForm.Show();
            }
        }
        private Color neddChangeColor = SystemColors.InactiveCaption;
        private Color backColor = SystemColors.ActiveCaption;
        private int logTime = 0;
        /// <summary>
        /// 第一次登录
        /// </summary>
        bool IsFirstLog = true;

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void MainFormWG_FormClosed(object sender, FormClosedEventArgs e)
        {
            //GlobalMotion.LightState(e_LightState.全灭);
            //displalyInfo.displayInfoMain($"已关蜂鸣器");
            
            //GlobalVar.isExit = true;
            //Thread.Sleep(100);
            //GlobalMotion.Card_1.StopAllAxis();
            //GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.on);
            //GlobalMotion.Card_1.setOut((ushort)e_output_card1.下层要料, e_out_OnOff.on);
            //displalyInfo.displayInfoMain($"已保存配置");

            //SerializeHelper.serializeFile(GlobalVar.closeSavePath, GlobalVar.closeSave, "已保存closeSave配置");
            //displalyInfo.displayInfoMain($"已保存closeSave配置");
            ////关灯
            //if (GlobalVar.cameraList != null && GlobalVar.cameraList.Count > 0)
            //{
            //    for (int i = 0; i < GlobalVar.cameraList.Count; i++)
            //    {
            //        if (GlobalVar.cameraList[i].isInitSuccess)
            //        {
            //            GlobalVar.cameraList[i].stopGrab();
            //            GlobalVar.cameraList[i].DisConnectDev();
            //        }
            //    }
            //    displalyInfo.displayInfoMain($"已关闭相机");
            //}
            //if (GlobalVar.RFIDBox_in != null && GlobalVar.RFIDBox_in.IsConnect)
            //    GlobalVar.RFIDBox_in.disconnectRFID();
            //if (GlobalVar.RFIDBox_out != null && GlobalVar.RFIDBox_out.IsConnect)
            //    GlobalVar.RFIDBox_out.disconnectRFID();

            //Process.GetCurrentProcess().Kill();
            //Application.Exit();
        }

        private void MainFormWG_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确认退出程序吗?", "操作提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;

                return;
            }

           // PageMain.CloseVisionWindow();

            GlobalMotion.LightState(e_LightState.全灭);
            displalyInfo.displayInfoMain($"已关蜂鸣器");

            GlobalVar.isExit = true;
            Thread.Sleep(100);
            GlobalMotion.Card_1.StopAllAxis();
            GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.on);
            GlobalMotion.Card_1.setOut((ushort)e_output_card1.下层要料, e_out_OnOff.on);
            displalyInfo.displayInfoMain($"已保存配置");

            SerializeHelper.serializeFile(GlobalVar.closeSavePath, GlobalVar.closeSave, "已保存closeSave配置");
            displalyInfo.displayInfoMain($"已保存closeSave配置");

            //20241207修改更换海康相机屏蔽现有相机连接
            //if (GlobalVar.cameraList != null && GlobalVar.cameraList.Count > 0)
            //{
            //    for (int i = 0; i < GlobalVar.cameraList.Count; i++)
            //    {
            //        if (GlobalVar.cameraList[i].isInitSuccess)
            //        {
            //            GlobalVar.cameraList[i].stopGrab();
            //            GlobalVar.cameraList[i].DisConnectDev();
            //        }
            //    }
            //    displalyInfo.displayInfoMain($"已关闭相机");
            //}

            //RFID断开连接
            if (GlobalVar.RFIDBox_in != null && GlobalVar.RFIDBox_in.IsConnect)
                GlobalVar.RFIDBox_in.disconnectRFID();
            if (GlobalVar.RFIDBox_out != null && GlobalVar.RFIDBox_out.IsConnect)
                GlobalVar.RFIDBox_out.disconnectRFID();
            //关灯
            GlobalMotion.LightState(e_LightState.全灭);
            Process.GetCurrentProcess().Kill();
            Application.Exit();
        }

    }

    public enum e_ButtonMain
    {
        主界面,
        设备参数,
        手动界面,
        工艺配置,
        报警界面,
        IO查询,
        产品切换,
        产品数据,
        权限设定,
        功能参数
    }
}
